
TEX=$(shell which pdflatex)
BIBTEX=$(shell which bibtex)

NAME=main

.PHONY: clean clean_all

all: ${NAME}.pdf

clean_all: clean
	if [ -e '${NAME}.pdf' ]; then rm -f ${NAME}.pdf; fi

clean:
	if [ -e '${NAME}.aux' ]; then rm -f *.aux; fi
	if [ -e '${NAME}.log' ]; then rm -f ${NAME}.log; fi
	if [ -e '${NAME}.toc' ]; then rm -f ${NAME}.toc; fi
	if [ -e '${NAME}.idx' ]; then rm -f ${NAME}.idx; fi
	if [ -e '${NAME}.ind' ]; then rm -f ${NAME}.ind; fi
	if [ -e '${NAME}.ilg' ]; then rm -f ${NAME}.ilg; fi
	if [ -e '${NAME}.lot' ]; then rm -f ${NAME}.lot; fi	
	if [ -e '${NAME}.lof' ]; then rm -f ${NAME}.lof; fi	
	if [ -e '${NAME}.bbl' ]; then rm -f ${NAME}.bbl; fi	
	if [ -e '${NAME}.blg' ]; then rm -f ${NAME}.blg; fi	
	if [ -e '${NAME}.brf' ]; then rm -f ${NAME}.brf; fi	
	if [ -e '${NAME}.out' ]; then rm -f ${NAME}.out; fi	

${NAME}.pdf: ${NAME}.tex ${NAME}.ind bibliography.bbl
	${TEX} ${NAME}.tex
	${TEX} ${NAME}.tex # to be shure of toc and links

${NAME}.ind: ${NAME}.idx
	makeindex ${NAME}

${NAME}.idx: ${NAME}.tex
	${TEX} ${NAME}.tex

bibliography.bbl:
	${BIBTEX} ${NAME}
